package com.ruoyi.web.controller.baodao.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.baodao.entity.*;
import com.ruoyi.baodao.mapper.BDMapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 演示 数据资产验真
 */
@Slf4j
@Controller
@RequestMapping("baodao/reviewzcgl")
public class ReviewZCGLController extends BaseController {
    @Autowired
    BDMapper bdMapper;
    private String prefix = "baodao/reviewzcgl";

    @GetMapping()
    public String operlog()
    {
        return prefix + "/reviewzcgl";
    }

    // 查询 追溯小票复核数据
    /**
     * 药品主档
     */
    @PostMapping("/list")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo list(String sellStartTime,String sellEndTime, String flag)

    {

        logger.info("查询追溯监管复核，时间参数：" + sellStartTime+" and "+ sellEndTime);
        // 获取 用户名 和  用户 部门
        // List<MFGoodsLink> mfGoodsLinks = mfGoodsLinkMapper.selectByExample(new MFGoodsLinkExample());


        // 取部门编号（门店号）
        /*SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();*/



        startPage(); //分页

        List<ZCGLReview> zsReviewList =  bdMapper.queryZCGLReview();

       return getDataTable(zsReviewList);

    }

    /**
     * 查看详细信息
     */
    // 增加药品关系
    @PostMapping("/remove")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult remove(@Param("sellNos") String sellNos){


        // 数据处理,
        // 以逗号分隔开的字符串转换成数组
        String[] key1Array = sellNos.split(",");


        for (String sellNo : key1Array ){

            try {
                bdMapper.updateZCGLFlag(sellNo);
            }catch (Exception e){
                e.printStackTrace();
                return error("异常");
            }
        }
        return success("成功");
    }

    /**
     * 查看详细信息
     */
    @GetMapping("/detail/{sellNo}")
    public String detail(@PathVariable("sellNo") String sellNo,String startTime,String endTime, ModelMap modelMap)
    {



        return prefix + "/detail";
    }


    public String getRecordUrl(String happened_time){
        // 上传文件路径
        String apiUrl = RuoYiConfig.getApiurl();
        //apiUrl = "http://10.0.1.116:30003";

        try {

            // 获取回放视频片段url
            Map<String, String> params = new HashMap<>();
            params.put("happened_time", happened_time);
            String result = HttpUtils.doPost(apiUrl + "/api_sunmi/getRecordUrl.aspx", params);

            JSONObject ret = JSON.parseObject(result);
            if(ret.getInteger("code").equals(0)){
                String recordUrl = ret.getString("recordUrl");
                return recordUrl;
            }
        } catch (Exception ex) {

        }
        return "";

    }



    /**
     * 小票数据
     */
    @PostMapping("/list/receipt")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo receiptlist(String sellNo,String startTime, String endTime)

    {

        logger.info("查询小票打印数据，参数：" + sellNo );
        // 获取 用户名 和  用户 部门
        // List<MFGoodsLink> mfGoodsLinks = mfGoodsLinkMapper.selectByExample(new MFGoodsLinkExample());


        // 取部门编号（门店号）
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();

        startPage(); //分页
        List<PrintReceiptDetail> printReceiptDetailList =  bdMapper.selectPrintReceiptDetailList(shopId,sellNo);

        return getDataTable(printReceiptDetailList);

    }

    /**
     * 药品主档
     */
    @PostMapping("/list/zsRecord")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo zsRecordlist(String sellNo,String startTime, String endTime)

    {

        logger.info("查询追溯监管复核，时间参数：" + startTime+" and "+ endTime);
        // 获取 用户名 和  用户 部门
        // List<MFGoodsLink> mfGoodsLinks = mfGoodsLinkMapper.selectByExample(new MFGoodsLinkExample());


        // 取部门编号（门店号）
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();

        startPage(); //分页
        List<EvtRecordSale> evtRecordSaleList =  bdMapper.selectEvtRecordSaleList(shopId,startTime,endTime);

        return getDataTable(evtRecordSaleList);

    }

}
